<?php

/**
 *
 */
class Tri_View_Helper_Thumbnail extends Zend_View_Helper_Abstract
{
    private $_name   = null;
    private $_width  = null;
    private $_height = null;
    private $_type   = array('micro'  => array('width' => 40, 'height' => 40),
                             'small'  => array('width' => 50, 'height' => 50),
                             'medium' => array('width' => 150, 'height' => 150));

    const IMAGETYPE_BMP  = 'image/bmp';
    const IMAGETYPE_GIF  = 'image/gif';
    const IMAGETYPE_JPG  = 'image/jpg';
    const IMAGETYPE_JPEG = 'image/jpeg';
    const IMAGETYPE_PNG  = 'image/png';
    const NO_IMAGE = "noimage";

    public function thumbnail($path, $type, $sex)
    {
        $uploadDir = PUBLIC_PATH . '/upload/';
        if($path){
            $aux = explode('.', $path);
            $thumb = $aux[0] . '-' . $type;
        } else {
            if($sex=='F'){$path='user_female';} else {$path = 'user_male';}
            $thumb = $path. '-' . $type;
        }
        if (!file_exists($uploadDir . $thumb)) {
            $this->_open($uploadDir . $path);
            if ($this->_image == self::NO_IMAGE) {
                $identity  = Zend_Auth::getInstance()->getIdentity();
                if($identity->sex=='F'){ return PUBLIC_PATH . '/upload/user_female.png';}
                else { return PUBLIC_PATH . '/upload/user_male.png';}
            }
            $this->_resize($this->_type[$type]['width'], $this->_type[$type]['height']);
           
            switch($this->_mimeType) {
                case self::IMAGETYPE_BMP:
                    imagebmp($this->_image, $uploadDir . $thumb);
                    break;
                case self::IMAGETYPE_GIF:
                    imagegif($this->_image, $uploadDir . $thumb);
                    break;
                case self::IMAGETYPE_JPEG:
                case self::IMAGETYPE_JPG:
                    imagejpeg($this->_image, $uploadDir . $thumb, 90);
                    break;
                case self::IMAGETYPE_PNG:
                    imagepng($this->_image, $uploadDir . $thumb);
                    break;
            }
        }
        return $thumb;
    }

    public function resize_max($path)
    {
        $publicDir = PUBLIC_PATH;
        if (file_exists($publicDir.'/tmp/'.$path)) {
            $this->_open($publicDir.'/tmp/'.$path);

            $this->_resize(600,450);
           
            switch($this->_mimeType) {
                case self::IMAGETYPE_BMP:
                    imagebmp($this->_image, $publicDir.'/tmp/'.$path);
                    break;
                case self::IMAGETYPE_GIF:
                    imagegif($this->_image, $publicDir.'/tmp/'.$path);
                    break;
                case self::IMAGETYPE_JPEG:
                case self::IMAGETYPE_JPG:
                    imagejpeg($this->_image, $publicDir.'/tmp/'.$path, 100);
                    break;
                case self::IMAGETYPE_PNG:
                    imagepng($this->_image, $publicDir.'/tmp/'.$path);
                    break;
            }
        }
    }

    public function crop($path, $bounds)
    {
        $publicDir = PUBLIC_PATH;
        if (file_exists($publicDir.'/tmp/'.$path)) {

            $this->_open($publicDir.'/tmp/'.$path);

            $maxWidth = $maxHeight = 150;
            $newWidth  = $this->_setDimension('w', $maxWidth, $maxHeight);
            $newHeight = $this->_setDimension('h', $maxWidth, $maxHeight);

            $newImage   = imagecreatetruecolor($maxWidth, $maxHeight);
            $background = imagecolorallocate($newImage, 0, 0, 0);

            imagecolortransparent($newImage, $background);
            imagealphablending($newImage, false);
            imagesavealpha($newImage, true);

            imagecopyresampled($newImage, $this->_image, 0, 0, $bounds['x'], $bounds['y'], $maxWidth, $maxHeight, $bounds['w'], $bounds['h']);

            $this->_image = $newImage;

//            $this->_resize($maxWidth,$maxHeight);
           
            switch($this->_mimeType) {
                case self::IMAGETYPE_BMP:
                    imagebmp($this->_image, $publicDir.'/upload/'.$path);
                    break;
                case self::IMAGETYPE_GIF:
                    imagegif($this->_image, $publicDir.'/upload/'.$path);
                    break;
                case self::IMAGETYPE_JPEG:
                case self::IMAGETYPE_JPG:
                    imagejpeg($this->_image, $publicDir.'/upload/'.$path, 90);
                    break;
                case self::IMAGETYPE_PNG:
                    imagepng($this->_image, $publicDir.'/upload/'.$path);
                    break;
            }
            
            unlink($publicDir.'/tmp/'.$path);
            unlink($publicDir.'/upload/'.$path.'-micro');
            unlink($publicDir.'/upload/'.$path.'-small');
            unlink($publicDir.'/upload/'.$path.'-medium');
        }
    }

    protected function _setInfo($path)
    {
        $imgSize = @getimagesize($path);
        if(!$imgSize || ($imgSize[0] == 0 || $imgSize[1] == 0)) {
            $this->_width    = self::NO_IMAGE;
            $this->_height   = self::NO_IMAGE;
            $this->_mimeType = self::NO_IMAGE;
            return;
        } 
        $this->_width    = $imgSize[0];
        $this->_height   = $imgSize[1];
        $this->_mimeType = $imgSize['mime'];
    }

    protected function _setDimension($forDim, $maxWidth, $maxHeight)
    {
        if ($this->_width > $maxWidth) {
            $ration = $maxWidth/$this->_width;
            $newwidth = round($this->_width*$ration);
            $newheight = round($this->_height*$ration);

            if ($newheight > $maxHeight) {
                $ration = $maxHeight/$newheight;
                $newwidth = round($newwidth*$ration);
                $newheight = round($newheight*$ration);

                if ($forDim == 'w') {
                    return $newwidth;
                } else {
                    return $newheight;
                }
            } else {
                if ($forDim == 'w') {
                    return $newwidth;
                } else {
                    return $newheight;
                }
            }
        } else if ($this->_height > $maxHeight) {
            $ration = $maxHeight/$this->_height;
            $newwidth = round($this->_width*$ration);
            $newheight = round($this->_height*$ration);
            if ($newwidth > $maxWidth) {
                $ration = $maxWidth/$newwidth;
                $newwidth = round($newwidth*$ration);
                $newheight = round($newheight*$ration);
                if ($forDim == 'w') {
                    return $newwidth;
                } else {
                    return $newheight;
                }
            } else {
                if ($forDim == 'w') {
                    return $newwidth;
                } else {
                    return $newheight;
                }
            }
        } else {
            if($forDim == 'w') {
                return $this->_width;
            } else {
                return $this->_height;
            }
        }
    }

    protected function _open($path)
    {
        
        $this->_setInfo($path);
        
        switch($this->_mimeType) {
            case self::IMAGETYPE_BMP:
                $this->_image = imagecreatefrombmp($path);
                break;
            case self::IMAGETYPE_GIF:
                $this->_image = imagecreatefromgif($path);
                break;
            case self::IMAGETYPE_JPEG:
            case self::IMAGETYPE_JPG:
                $this->_image = imagecreatefromjpeg($path);
                break;
            case self::IMAGETYPE_PNG:
                $this->_image = imagecreatefrompng($path);
                break;
            default:
                $this->_image = self::NO_IMAGE;
                break;
        }
    }

    public function _resize($maxWidth, $maxHeight)
    {
        $newWidth  = $this->_setDimension('w', $maxWidth, $maxHeight);
        $newHeight = $this->_setDimension('h', $maxWidth, $maxHeight);

        $newImage   = imagecreatetruecolor($newWidth, $newHeight);
        $background = imagecolorallocate($newImage, 0, 0, 0);

        imagecolortransparent($newImage, $background);
        imagealphablending($newImage, false);
        imagesavealpha($newImage, true);

        imagecopyresampled($newImage, $this->_image, 0, 0, 0, 0, $newWidth, $newHeight, $this->_width, $this->_height);

        $this->_image = $newImage;
    }
}
